package team.bluepen.order.data.database.mysql;

import space.lingu.light.Database;
import space.lingu.light.Light;
import space.lingu.light.LightDatabase;
import space.lingu.light.log.JdkDefaultLogger;
import team.bluepen.order.data.database.OrderConnectionPool;
import team.bluepen.order.data.entity.User;
import team.bluepen.order.util.Log;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author Kai
 */
@Database(name = "order_system", version = 1, tables = {User.class},
        datasourceConfig = "/user.properties")
public abstract class UserDatabase extends LightDatabase {
    // 用户数据使用MySQL储存
    private static final String TAG = "OrderDatabase";

    public abstract UserDao getUserDao();

    @Override
    public Connection requireConnection() {
        Connection connection = super.requireConnection();
        try {
            PreparedStatement useStmt =
                connection.prepareStatement(
                        "USE order_system");
            useStmt.execute();
            useStmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return connection;
    }

    public void init() {
        try {
            Connection connection = super.requireConnection();
            PreparedStatement dbStmt =
                    connection.prepareStatement(
                            "CREATE DATABASE IF NOT EXISTS order_system");
            dbStmt.execute();
            dbStmt.close();
            PreparedStatement useStmt =
                    connection.prepareStatement(
                            "USE order_system");
            useStmt.execute();
            useStmt.close();
            PreparedStatement createUserStmt =
                    connection.prepareStatement(
                            "CREATE TABLE IF NOT EXISTS user" +
                                    " (user_id int AUTO_INCREMENT, " +
                                    "username varchar(100)," +
                                    "user_password varchar(100)," +
                                    "user_role varchar(20)," +
                                    " PRIMARY KEY (user_id)" +
                                    ") ENGINE=InnoDB DEFAULT CHARSET=utf8");
            createUserStmt.execute();
            createUserStmt.close();
            releaseConnection(connection);
        } catch (SQLException e) {
            Log.e(TAG, "create table failed.");
        }
    }

    public UserDatabase() {
    }

    private static volatile UserDatabase DATABASE;

    public static UserDatabase getDatabase() {
        if (DATABASE == null) {
            synchronized (UserDatabase.class) {
                if (DATABASE == null) {
                    DATABASE = Light.databaseBuilder(UserDatabase.class,
                                    MysqlDialectProvider.class)
                            .setConnectionPool(OrderConnectionPool.class)
                            .setLogger(JdkDefaultLogger.getLogger("Order"))
                            .build();
                }
            }
        }
        DATABASE.init();
        return DATABASE;
    }

}
